#include "i2cDriver.h"
#include <util/delay_basic.h>

#include <avr/io.h>

// Initialize the i2c interface
void i2cInit(void)
{
	TWSR = 0x00;
	TWBR = 0xA; // SCL frequency
	TWCR = 0x04;
}

void i2cStart(void)
{
	TWCR = (1 << TWINT) | (1 << TWSTA) | (1<< TWEN);
	while (!(TWCR & (1<<TWINT)));
}

void i2cWrite(unsigned char data)
{
	TWDR = data;
	TWCR = (1 << TWINT) | (1 << TWEN);
	while (!(TWCR & ( 1 << TWINT)));
}

unsigned char i2cRead(unsigned char ackVal)
{
	TWCR = ( 1 << TWINT) | (1 << TWEN) | ( ackVal << TWEA);
	while( ! (TWCR 	& (1 << TWINT)));
	return TWDR;
}

void i2cStop()
{

	TWCR = (1<< TWINT) | (1 << TWEN) | (1 << TWSTO);
	_delay_loop_1(100);
}

